package creature;

import com.sun.corba.se.impl.ior.WireObjectKeyTemplate;

import toxi.geom.Vec2D;

public class CircleLayout extends Layout
{ 
	public CircleLayout(int _numParticles)
	{
		super(_numParticles);
		type = TYPE_CIRCLE;
	}
	
	public void generate(float _x, float _y, float _radius)
	{
		generate(new Vec2D(_x, _y), _radius);
	}
	
	public void generate(Vec2D _pos, float _radius)
	{
		pos = _pos;
		
		float aDif = (float)(2.0f * Math.PI) / numNodes;
		float angle = (float)Math.PI + aDif * 0.5f;
		float tx;
		float ty;
		
		for (int i = 0; i < numNodes; i++)
		{
			tx = pos.x + _radius * (float)Math.cos(angle);
			ty = pos.y + _radius * (float)Math.sin(angle);
			
			particlePositions[i] = new Vec2D(tx, ty);
			
			angle += aDif;
			angle %= 2.0f * Math.PI;
		}
		
		float nodeSpacing = getChordLength(_radius, aDif);
		for (int i = 0; i < numNodes; i++)
		{
			springLengths[i] = nodeSpacing;
		}
		
		int writeIndex = numNodes;
		for (int i = 0; i < numNodes / 2; i++)
		{
			float d = particlePositions[i].sub(particlePositions[(numNodes-1)-i]).magnitude();
			springLengths[writeIndex] = d;
			writeIndex++;
		}
		
		float dax1 = particlePositions[0].sub(particlePositions[numNodes/2]).magnitude();
		springLengths[writeIndex] = dax1;
		writeIndex++;
		
		float dax2 = particlePositions[numNodes/2 - 1].sub(particlePositions[numNodes-1]).magnitude();
		springLengths[writeIndex] = dax2;
		writeIndex++;
		
		for (int i = 0; i < numNodes; i++)
		{
			float dist = pos.sub(particlePositions[i]).magnitude();
			springLengths[writeIndex] = dist;
			writeIndex++;
		}
		
//		for (int i = 0; i < springLengths.length; i++)
//		{
//			System.out.println(springLengths[i]);
//		}
	}
	
	private float getChordLength(float _radius, float _angle)
	{
		return 2.0f * _radius * (float) Math.sin(_angle * 0.5f);
	}
}
